package domain.shop;

import domain.AbstractEntity;

import javax.persistence.Entity;
import javax.persistence.OneToOne;
import java.math.BigDecimal;

/**
 * Product added into Basket.
 */

@Entity
public class BasketItem extends AbstractEntity {

    @OneToOne
    Product product;
    BigDecimal price;
    Integer multiplicity;

    public BasketItem() {
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public Integer getMultiplicity() {
        return multiplicity;
    }

    public void setMultiplicity(Integer multiplicity) {
        this.multiplicity = multiplicity;
    }

    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    public void setName(String name) {
        product.setName(name);
    }

    public String getName() {
        return product.getName();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof BasketItem)) return false;

        BasketItem that = (BasketItem) o;

        if (multiplicity != null ? !multiplicity.equals(that.multiplicity) : that.multiplicity != null) return false;
        if (price != null ? !price.equals(that.price) : that.price != null) return false;
        if (product != null ? !product.equals(that.product) : that.product != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = product != null ? product.hashCode() : 0;
        result = 31 * result + (price != null ? price.hashCode() : 0);
        result = 31 * result + (multiplicity != null ? multiplicity.hashCode() : 0);
        return result;
    }
}

